package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import qlsl.androiddesign.activity.subactivity.CarDetailActivity;
import qlsl.androiddesign.adapter.subadapter.CarDetailAdapter;
import qlsl.androiddesign.application.SoftwareApplication;
import qlsl.androiddesign.entity.otherentity.CarDetail;
import qlsl.androiddesign.entity.otherentity.FileType;
import qlsl.androiddesign.http.service.subservice.CarService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshScrollView;
import qlsl.androiddesign.util.runfeng.ToActivityUtil;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.indicatorview.ImageIndicatorView.OnItemClickListener;
import qlsl.androiddesign.view.indicatorview.IndicatorView;

/**
 * 二手车详情页<br/>
 * 需要传入的键：data<br/>
 * 传入的值类型： UsedCar<br/>
 * 传入的值含义：二手车<br/>
 * 是否必传 ：是
 */
public class CarDetailView extends FunctionView<CarDetailActivity> implements OnItemClickListener {

	private PullToRefreshScrollView refreshView;

	private ListView listView;

	private IndicatorView indicatorView;

	private CarDetail item;

	public CarDetailView(CarDetailActivity activity) {
		super(activity);
		setContentView(R.layout.activity_car_detail);
	}

	protected void initView(View view) {
		item = (CarDetail) activity.getIntent().getSerializableExtra("data");
		setTitle("详情");
		refreshView = findViewById(R.id.refreshScrollView);
		listView = findViewById(R.id.listView);
		indicatorView = findViewById(R.id.net_indicate_view);
		indicatorView.setIndicateStyle(IndicatorView.INDICATE_ROUND_STYLE);
		indicatorView.setupLayoutByDefaultResource(1);
		indicatorView.show();
		setListViewData();
	}

	protected void initData() {
		CarService.queryUsedCarPhotoList(item.getUsedcarid(), this, activity);
	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
		indicatorView.setOnItemClickListener(this);
	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof List) {
			List<FileType> list = (List<FileType>) t[0];
			setImages(list);
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_store:
			doClickStoreView();
			break;
		case R.id.btn_buy:
			doClickPhoneNumberView();
			break;
		}
	}

	private void doClickStoreView() {
		CarService.storeUsedCar(item.getUsedcarid(), this, activity);
	}

	private void doClickPhoneNumberView() {
		if (!TextUtils.isEmpty(item.getMobile())) {
			ToActivityUtil.callPhoneActivity(activity, item.getMobile());
		} else {
			showToast("商家未提供电话号码");
		}

	}

	private void setImages(List<FileType> list) {
		ArrayList<String> urls = new ArrayList<String>();
		for (FileType item : list) {
			if (item.getType().equals("IMAGE")) {
				urls.add(item.getUrl());
			}
		}
		indicatorView.setupLayout(urls);
		indicatorView.setTag(urls);
		indicatorView.show();
	}

	private void setListViewData() {
		String single = activity.getString(R.string.single);
		String[] names = new String[] { "机型", "品牌", "型号", "设备所在地", "价格", "使用小时数", "出厂年限", "发布日期", "联系人", "联系电话" };
		String[] contents = new String[] { item.getMachinevision(), item.getBrands(), item.getModelno(),
				item.getProvince() + single + item.getCity() + single + item.getArea(), item.getPrice(),
				item.getUsedtime(), item.getYears(), item.getTime(), item.getContacter(), item.getMobile() };
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		for (int index = 0; index < names.length; index++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", names[index]);
			map.put("content", contents[index]);
			list.add(map);
		}
		BaseAdapter adapter = new CarDetailAdapter(activity, list);
		listView.setAdapter(adapter);
	}

	private OnRefreshListener<ScrollView> refreshListener = new OnRefreshListener<ScrollView>() {
		public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
			refreshView.setRefreshingLabel(
					SoftwareApplication.getInstance().getResources().getString(R.string.refreshing_label));
			refreshView.onLoadComplete();
		}

	};

	public void OnItemClick(View view, int position) {
		ArrayList<String> urls = (ArrayList<String>) indicatorView.getTag();
		startActivityToPhotoBrowseActivity(position, null, urls);
	}

}
